package psn.zjk.exporter.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.MessageFormat;

/**
 *
 * @author PojoEnhancer
 * @CREATE_TIME 2020/11/14 15:11
 */
public class PojoEnhancer {

    public static Object getProperty(Object o, String field) {
        Object out = null;
        try {
            String methodName1 = generateMethodName("is", field);
            String methodName2 = generateMethodName("get", field);
            Method method=null;
            for (Method method2 : o.getClass().getMethods()) {
                if (method2.getName().equals(methodName1)||method2.getName().equals(methodName2)){
                    method=method2;
                    break;
                }
            }
            if (method == null) {
                throw new IllegalArgumentException("不存在属性：" + field);
            }
            out = method.invoke(o);
        } catch (Exception e) {
        }
        return out;
    }

    private static String generateMethodName(String prefix, String fieldName) {
        return prefix + (char) (fieldName.charAt(0) - 32) + fieldName.substring(1);
    }
}